﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.LevelDataAccess;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class DeptManager : System.Web.UI.Page
    {
        BLL_Department deptBLL = null;
        List<Entity_USER_Department> listDeptAll = null;
        //List<Entity_USER_Department> listDeptByCondition = null;
        //Entity_USER_Department deptInfo = null;
        private static bool blBtnSelectedDept = false;   //确定最后操作的按钮是条件搜索部门还是获取所有部门.
        private static bool blControlValueCheck = false; //确定控件值(用户输入)正确性验证后再进行操作.
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            listDeptAll = new BLL_Department().getDeptList();
            deptBLL = new BLL_Department();
            if (!Page.IsPostBack)
            {
                gridDataBind();
                operationFeedbackByLabel("数据已加载完毕.");
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }
        }

        //操作状态显示
        private void operationFeedbackByLabel(string strFeedback)
        {
            lblOperationState.Text = "操作状态:";
            lblOperationState.Text += strFeedback;
        }

        //为GridViewDeptList绑定所有部门.
        private bool gridDataBind()
        {
            try
            {
                bool flag = false;
                if (listDeptAll != null)
                {
                    gridDataBind(listDeptAll);
                }
                else
                {
                    gridDataBind(deptBLL.getDeptList());    //改ver01.
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //根据传入list为GridViewDeptList绑定部门.
        private bool gridDataBind(object objDataSource)
        {
            try
            {
                this.GridViewDeptList.DataSource = (List<Entity_USER_Department>)objDataSource;
                this.GridViewDeptList.DataBind();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //使用自定义数据源,重新设置PageIndexChanging事件为实现分页正常.
        protected void GridViewDeptList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (blBtnSelectedDept)  //true:获取符合查询条件的部门给GridViewDeptList绑定数据源.false:获取所有部门给GridViewDeptList绑定数据源.
            {
                Entity_USER_Department department = new Entity_USER_Department();
                department.DeptName = this.txtSelectDeptName.Text.Trim();
                department.RecordStatus = Convert.ToDecimal(this.ddlSelectDeptState.SelectedValue);
                GridViewDeptList.PageIndex = e.NewPageIndex;
                gridDataBind(new BLL_Department().getDeptByCondition(department));
            }
            else
            {
                GridViewDeptList.PageIndex = e.NewPageIndex;
                gridDataBind(new BLL_Department().getDeptList());
            }
        }

        //GridView行中的选择部门按钮:
        protected void btnSelectedDept_Click(object sender, EventArgs e)
        {
            this.lblMsDeptState.Text = "";
            this.lblMsDeptName.Text = "";
            this.btnDelete.Enabled = true;
            this.btnSave.Text = "修   改";
            this.btnSave.Enabled = true;
            string strDeptID = ((Button)sender).CommandArgument.ToString(); //获取选定主键值.
            decimal dclDeptID = Convert.ToDecimal(strDeptID.Trim());
            Entity_USER_Department department = deptBLL.getDeptByDeptID(dclDeptID);
            lblDeptIDValue.Text = department.DeptID.ToString();
            txtDeptName.Text = department.DeptName;
            txtDeptDesc.Text = department.DeptDesc;
            RadioButtonListDeptState.SelectedValue = department.RecordStatus.ToString();
            operationFeedbackByLabel("已选择部门-"+department.DeptName +",系统等待进行下一步操作.");
        }

        //部门添加按钮:
        protected void btnAddDept_Click(object sender, EventArgs e)
        {
            operationFeedbackByLabel("正在创建新部门...");
            this.lblDeptIDValue.Text = "";
            this.txtDeptName.Text = "";
            this.lblMsDeptName.Text = "*部门名称为必填项.";
            this.txtDeptDesc.Text = "";
            this.RadioButtonListDeptState.SelectedValue = "1";
            this.lblMsDeptState.Text = "*新增部门默认为启用状态";
            this.btnDelete.Enabled = false;
            this.btnSave.Text = "保   存";
            this.btnSave.Enabled = true;
        }

        //条件搜索按钮:
        protected void btnSelectByCondition_Click(object sender, EventArgs e)
        {
            blBtnSelectedDept = true;
            Entity_USER_Department deptCondition = new Entity_USER_Department();
            deptCondition.DeptName = this.txtSelectDeptName.Text.Trim();
            deptCondition.RecordStatus = Convert.ToDecimal(this.ddlSelectDeptState.SelectedValue.Trim());
            AppLog.Write("deptRecordstate:" + deptCondition.RecordStatus.ToString(), SysTools.AppLog.LogMessageType.Debug);
            List<Entity_USER_Department> deptConditionList = new BLL_Department().getDeptByCondition(deptCondition);
            if (deptConditionList.Count.Equals(0))
            {
                //blBtnSelectedDept = false;
                //gridDataBind();
                gridDataBind(null);
                operationFeedbackByLabel("没有符合条件的部门,请检查搜索条件.");
            }
            else
            {
                if (gridDataBind(deptConditionList))
                {
                    operationFeedbackByLabel("已加载符合条件的"+deptConditionList.Count.ToString()+"个部门.");
                }
            }
        }

        //所有部门按钮:
        protected void btnSelectDeptAll_Click(object sender, EventArgs e)
        {
            blBtnSelectedDept = false;            
            gridDataBind();
            operationFeedbackByLabel("已加载所有部门.");
        }

        //保存部门按钮:
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (blControlValueCheck)
            {

                if (this.lblDeptIDValue.Text != "" && !Convert.ToDecimal(this.lblDeptIDValue.Text).Equals(0))   //lblDeptIDValue默认为0且选择部门后值不为0)
                {
                    decimal deptID = Convert.ToDecimal(this.lblDeptIDValue.Text);
                    Entity_USER_Department department = new Entity_USER_Department();
                    department.DeptName = this.txtDeptName.Text.Trim();
                    department.DeptDesc = this.txtDeptDesc.Text.Trim();
                    department.RecordStatus = Convert.ToDecimal(this.RadioButtonListDeptState.SelectedValue);
                    if (deptBLL.updateDeptByDeptIDAndEntity(deptID, department))
                    {
                        GridViewDeptListBindSetting();
                        operationFeedbackByLabel("成功修改部门:" + department.DeptName + ".");
                        this.lblDeptIDValue.Text = "";
                        this.txtDeptName.Text = "";
                        this.txtDeptDesc.Text = "";
                        this.RadioButtonListDeptState.SelectedValue = "1";
                        this.btnDelete.Enabled = false;
                        this.btnSave.Enabled = false;
                        this.lblDeptIDValue.Text = "";
                    }
                    else
                    {
                        operationFeedbackByLabel("修改部门 " + department.DeptName + " 失败.");
                    }
                }
                else
                {
                    Entity_USER_Department department = new Entity_USER_Department();
                    department.DeptName = this.txtDeptName.Text.Trim();
                    department.DeptDesc = this.txtDeptDesc.Text.Trim();
                    department.RecordStatus = Convert.ToDecimal(this.RadioButtonListDeptState.SelectedValue);
                    if (deptBLL.addDept(department))
                    {
                        GridViewDeptListBindSetting();
                        operationFeedbackByLabel("成功新增部门:" + department.DeptName + ".");
                        this.lblDeptIDValue.Text = "";
                        this.txtDeptName.Text = "";
                        this.txtDeptDesc.Text = "";
                        this.RadioButtonListDeptState.SelectedValue = "1";
                        this.lblDeptIDValue.Text = "";
                        this.btnDelete.Enabled = false;
                        this.btnSave.Enabled = false;
                    }
                    else
                    {
                        operationFeedbackByLabel("新增部门 " + department.DeptName + " 失败.");
                    }
                }
            }
            else
            {
                operationFeedbackByLabel("请检查输入值的正确性.");
            }
        }

        //删除部门按钮:(验证部门下是否存在用户.)
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            decimal dclDeleteDeptID = Convert.ToDecimal(this.lblDeptIDValue.Text.Trim());
            Entity_USER_Department deptEntity = deptBLL.getDeptByDeptID(dclDeleteDeptID);
            int iUserCountByDept = 0;
            iUserCountByDept = deptBLL.getUserCountByDeptID(dclDeleteDeptID);
     //AppLog.Write("userCount:" + iUserCountByDept.ToString(), AppLog.LogMessageType.Debug);
            if (iUserCountByDept.Equals(0))
            {
                if (deptBLL.deleteDeptByDeptID(dclDeleteDeptID))    //如果删除成功.
                {
                    GridViewDeptListBindSetting();
                    operationFeedbackByLabel("已删除部门:" + deptEntity.DeptName + ".");
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.txtDeptDesc.Text = "";
                    this.txtDeptName.Text = "";
                    this.lblDeptIDValue.Text = "";
                }
                else
                {
                    operationFeedbackByLabel("删除失败.");
                }
            }
            else
            {
                operationFeedbackByLabel("部门:"+deptEntity.DeptName+"下存在多个用户,不能删除.");
            }
        }

        //根据当前情况重新绑定数据源:
        private void GridViewDeptListBindSetting()
        {
            string strDeptName = this.txtSelectDeptName.Text.Trim();
            decimal dclDeptState = Convert.ToDecimal(this.ddlSelectDeptState.SelectedValue);
            if (blBtnSelectedDept)  //最后一次是操作根据条件的搜索按钮.
            {
                Entity_USER_Department deptEntity = new Entity_USER_Department();
                if (strDeptName!="" && strDeptName!=null)
                {
                    deptEntity.DeptName = strDeptName;
                }
                deptEntity.RecordStatus = dclDeptState;
                gridDataBind(deptBLL.getDeptByCondition(deptEntity));
            }
            else
            {
                gridDataBind(deptBLL.getDeptList());
            }
        }

#region 验证控件输入值

        protected void txtDeptName_TextChanged(object sender, EventArgs e)
        {
            if (this.txtDeptName.Text.Length < 2 || this.txtDeptName.Text.Length > 16)
            {
                this.lblMsDeptName.Text = "*长度为2至16字符.";
                blControlValueCheck = false;
            }
            else
            {
                //验证数据库是否存在同名部门.
                Entity_USER_Department deptEntity = new Entity_USER_Department();
                deptEntity.DeptName = this.txtDeptName.Text.Trim();
                if (deptBLL.getDeptByCondition(deptEntity).Count != 0)
                {
                    operationFeedbackByLabel("已存在同名部门,请确认名称后继续操作.");
                    this.lblMsDeptName.Text = "*部门名称重复,请确认.";
                    blControlValueCheck = false;
                }
                else
                {
                    this.lblMsDeptName.Text = "*";
                    blControlValueCheck = true;
                }
            }
        }

#endregion

    }
}
